/*
 * @Author: your name
 * @Date: 2021-02-16 20:25:08
 * @LastEditTime: 2021-02-19 20:36:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \demo_2021021601\include\common\my_printf.c
 */

#include "my_vsprintf.h"
#include "my_string.h"

#define OUTBUFSIZE 512
#define INBUFSIZE 512

extern void my_fputc(unsigned char c);
extern unsigned char my_fgetc(void);

static char g_pcOutBuf[OUTBUFSIZE];
static char g_pcInBuf[INBUFSIZE];

int my_printf(const char *fmt, ...)
{
  int i;
  int len;
  my_va_list args;

  my_va_start(args, fmt);
  len = my_vsprintf(g_pcOutBuf, fmt, args);
  my_va_end(args);
  for (i = 0; i < my_strlen(g_pcOutBuf); i++)
  {
    my_fputc(g_pcOutBuf[i]);
  }
  return len;
}

int my_scanf(const char *fmt, ...)
{
  int i = 0;
  unsigned char c;
  my_va_list args;

  while (1)
  {
    c = my_fgetc();
    my_fputc(c);
    if ((c == 0x0d) || (c == 0x0a))
    {
      g_pcInBuf[i] = '\0';
      break;
    }
    else
    {
      g_pcInBuf[i++] = c;
    }
  }

  my_va_start(args, fmt);
  i = my_vsscanf(g_pcInBuf, fmt, args);
  my_va_end(args);

  return i;
}
